﻿using UnityEngine;

public static class UnityUtility
{
    public static void DrawCircle(Vector3 position, float radius)
    {
        float m_Theta = 0.1f;
        // 设置矩阵
        Matrix4x4 defaultMatrix = Gizmos.matrix;
        Gizmos.matrix = Matrix4x4.Translate(position);
        // 绘制圆环
        Vector3 beginPoint = Vector3.zero;
        Vector3 firstPoint = Vector3.zero;
        for (float theta = 0; theta < 2 * Mathf.PI; theta += m_Theta)
        {
            float x = radius * Mathf.Cos(theta);
            float z = radius * Mathf.Sin(theta);
            Vector3 endPoint = new Vector3(x, z, 0);
            if (theta == 0)
            {
                firstPoint = endPoint;
            }
            else
            {
                Gizmos.DrawLine(beginPoint, endPoint);
            }

            beginPoint = endPoint;
        }

        // 绘制最后一条线段
        Gizmos.DrawLine(firstPoint, beginPoint);
        // 恢复默认矩阵
        Gizmos.matrix = defaultMatrix;
    }

    public static void DrawRectangle (Vector3 center, Vector3 size)
    {
        Vector3 topLeft = center +  new Vector3(-0.5f * size.x, 0.5f * size.y, 0);
        Vector3 topRight = center +  new Vector3(0.5f * size.x, 0.5f * size.y, 0);
        Vector3 bottomRight = center +  new Vector3(0.5f * size.x, -0.5f * size.y, 0);
        Vector3 bottomLeft = center +  new Vector3(-0.5f * size.x, -0.5f * size.y, 0);
        Gizmos.DrawLine(bottomLeft, topLeft);
        Gizmos.DrawLine(topLeft, topRight);
        Gizmos.DrawLine(topRight, bottomRight);
        Gizmos.DrawLine(bottomRight, bottomLeft);
    }

    public static Color IntToColor(int r, int g, int b, int a = 255)
    {
        return new Color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
    }
}